package editor.weapon;

import java.awt.*;

import javax.swing.*;

import entity.weapons.Weapon;

import java.awt.event.*;
import java.io.File;

import loading.ImageLoader;

public class WeaponDialog extends JDialog implements ActionListener
{
	/**
	 */
	private static final long serialVersionUID = 1285890862662952223L;
	/**
	 */
	
	private JTextField name;
	private JTextField damage;
	
	private JLabel lblName;
	private JLabel lblDamage;
	
	private JButton loadIcon;
	private JButton loadImage;
	private JButton done;
	private JButton cancel;
	private ImageLoader imgIcon;
	private ImageLoader imgImage;
	
	private Weapon weapon;
	
	public WeaponDialog( JFrame parent )
	{
		super( parent, true );
		
		this.setSize( 300, 350 );
		
		name = new JTextField( "Input Name" );
		damage = new JTextField( "Input Damage" );
		
		lblName = new JLabel( "Name:" );
		lblDamage = new JLabel( "Damage:" );

		loadIcon = new JButton( "Load Icon..." );	
		loadImage = new JButton( "Load Image..." );	
		done = new JButton( "Done" );	
		cancel = new JButton( "Cancel" );
		
		GridBagLayout layout = new GridBagLayout();
		
		addComponent( lblName, 		1, 1, 1, 1, layout );
		addComponent( name, 		2, 1, 2, 1, layout );
		
		addComponent( lblDamage, 	1, 2, 1, 1, layout );
		addComponent( damage, 		2, 2, 2, 1, layout );
		

		addComponent( loadIcon, 	2, 3, 1, 1, layout );
		addComponent( loadImage, 	2, 4, 1, 1, layout );
		addComponent( done, 		1, 5, 1, 1, layout );
		addComponent( cancel, 		3, 5, 1, 1, layout );
		
		this.setLayout( layout );

		loadIcon.addActionListener( this );
		loadImage.addActionListener( this );
		done.addActionListener( this );
		cancel.addActionListener( this );
		
		this.setVisible( true );
	}
	
	public Weapon getWeapon()
	{
		return weapon;
	}
	
	private void addComponent( Component c, int col, int row, int width, int height, GridBagLayout layout )
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = col;
		gbc.gridy = row;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = 100;
		gbc.weighty = 100;
		layout.setConstraints( c, gbc );
		gbc.insets = new Insets( 5, 5, 5, 5 );
		this.add( c );
	}
	
	public void actionPerformed( ActionEvent e ) 
	{
		Object o = e.getSource();
		if( o.equals( loadIcon ) )
		{
			JFileChooser fc = new JFileChooser();
			fc.setCurrentDirectory( new File( "C:/Documents and Settings/Owner/Desktop/Projects/" ) );
			int returnVal = fc.showOpenDialog( this );
			
			if( returnVal == JFileChooser.APPROVE_OPTION )
			{
				imgIcon = new ImageLoader( fc.getSelectedFile().getAbsolutePath() );
			}
		}
		else if( o.equals( loadImage ) )
		{
			JFileChooser fc = new JFileChooser();
			fc.setCurrentDirectory( new File( "C:/Documents and Settings/Owner/Desktop/Projects/" ) );
			int returnVal = fc.showOpenDialog( this );
			
			if( returnVal == JFileChooser.APPROVE_OPTION )
			{
				imgImage = new ImageLoader( fc.getSelectedFile().getAbsolutePath() );
			}
		}
		else if( o.equals( done ) )
		{
			weapon = new Weapon( name.getText(), imgIcon, imgImage, Integer.valueOf( damage.getText() ) );
			
			this.dispose();
		}
		else if( o.equals( cancel ) )
		{
			weapon = null;
			
			this.dispose();
		}
	}
}
